import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:hbtrans_flutter/apis.dart';
import 'package:hbtrans_flutter/beans/orderlist_bean.dart';
import 'package:hbtrans_flutter/const.dart';
import 'package:hbtrans_flutter/routes.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';
import 'package:hbtrans_flutter/view/loading.dart';
import 'package:shared_preferences/shared_preferences.dart';

class OrderList extends StatefulWidget {
  final userId;
  final index;

  const OrderList(this.userId, this.index);

  @override
  _OrderListState createState() => _OrderListState();
}

class _OrderListState extends State<OrderList> {
  var userId = '';
  var items = List();
  LoadingDialog loadingDialog;

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    MyDio.getInstance().clearInstance();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getUserId();
    _order();
  }



  itemAll(){
    List<Widget> widgets=List();
    for(int i=0;i<items.length;i++){
      widgets.add(Card(
        margin: EdgeInsets.fromLTRB(15, 10, 15, 2),
        elevation: 1,
        color: Colors.white,
        child: GestureDetector(
          onTap: () {
            String route = '${Routes.pay}?orderId=${items[i].id}';
            Routes.router.navigateTo(context, route,
                replace: false, transition: TransitionType.fadeIn);
          },
          child: 
              Padding(padding: EdgeInsets.all(9),
              child:  Column(
                children: <Widget>[
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Row(
                        children: <Widget>[
                          Icon(
                            Icons.photo,
                            size: 32,
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Text(
                            items[i].schname,
                            style: TextStyle(
                                fontSize: 13, color: Color(0xff333333)),
                          )
                        ],
                      ),
                      Text(
                        items[i].orderStatus=='0'? '等待买家付款':'买家已付款',
                        style:
                        TextStyle(fontSize: 11, color: Color(0xffF44336)),
                      )
                    ],
                  ),
                  Container(
                      margin: EdgeInsets.only(top: 15),
                      child: GestureDetector(
                        onTap: () {},
                        child:
                        Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Text(items[i].classname,style: TextStyle(fontSize: 14,color: Color(0xffF333333)),),
                                Text('￥'+items[i].price,style: TextStyle(fontSize: 18,color: Color(0xffF44336)),),
                              ],
                            ),
                            SizedBox(height: 4,),
                            Text('车型：C1',style: TextStyle(fontSize: 12,color: Color(0xffF999999)),)
                          ],
                        ),

                      )
                  ),
                  SizedBox(height: 12,),
                  Container(
                    alignment: Alignment.centerRight,
                    child: Text(
                      '共计1件商品，合计￥'+items[i].tolalprice,
                      style: TextStyle(fontSize: 13, color: Color(0xff333333)),
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Container(
                      alignment: Alignment.centerRight,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.end,
                        children: <Widget>[
                          Container(
                            alignment: Alignment.center,
                            width: 60,
                            height: 23,
                            decoration: BoxDecoration(
                              border: Border.all(color: Color(0xff666666)),
                              borderRadius: BorderRadius.circular(15.0),
                            ),
                            child:GestureDetector(
                              onTap: (){
                                String route = '${Routes.modifyOrder}?orderId=${items[i].id}';
                                Routes.router.navigateTo(context, route,
                                    replace: false, transition: TransitionType.fadeIn);
                              },
                              child: Text(
                                '修改订单',
                                style: TextStyle(
                                    fontSize: 11, color: Color(0xff666666)),
                              ),
                            )

                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Container(
                            alignment: Alignment.center,
                            width: 50,
                            height: 22,
                            decoration: BoxDecoration(
                              border: Border.all(color: Color(0xffF44336)),
                              borderRadius: BorderRadius.circular(15.0),
                            ),
                            child: Text(
                              '付款',
                              style: TextStyle(
                                  fontSize: 11, color: Color(0xffF44336)),
                            ),
                          ),
                        ],
                      ))
                ],
              ),)
        ),
      ));
    }
    return widgets;
  }

  void getUserId() async {
    SharedPreferences sp = await SharedPreferences.getInstance();
    userId = sp.getString('id');
    print(userId);
  }


  _showDialog() {
    loadingDialog = LoadingDialog(
      text: '请稍候...',
    );
    showDialog<Null>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return loadingDialog;
        });
  }

   _order() async {
//    _showDialog();
    Map<String, dynamic> data = Map();
//    data['userId'] ='e8b13d88efd43633b01dc6cba1ac9707';
    data['userId'] =userId;
    data['orderStatus'] = widget.index;
    MyDio.getInstance(url: Const.TRANS_SERVER_PATH_NEW3).get(Apis.orderList,
        (data) {
      print(data);
//      if (loadingDialog != null) {
//        loadingDialog = null;
//        Navigator.pop(context);
//      }
          setState(() {
            items.clear();
            for (var s in data) {
              items.add(OrderListBean.fromJson(s));
            }
          });
    }, params: data);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body:ListView(
          children: itemAll(),
        )
      ),
    );
  }
}
